<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手写sleep函数</title>
</head>
<body>
<script>
    function sleep(ms) {
        let p;
        p = new Promise((resolve, reject) => {
            console.log(p);
            setTimeout(() => {
                // pending 等待
                console.log(p,'////');
                reject();
                console.log(p);
                //resolve();
                // 调用resolve函数后 fulfilled 成功
                console.log(p);
            }, ms);
        });
        return p;
    }

    sleep(3000)
    .then(() => {
        console.log('3秒后执行');
    })
    .catch(() => {
        console.log('3秒后执行失败');
    })
</script>
</body>
</html>